/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ixj.online.process;

import java.util.ArrayList;
import java.util.List;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

/**
 *
 * @author Administrator
 */
public class CustomerSAXHandler2 extends DefaultHandler {

    private List<Customer> listCustomer = null;
    //
    private Customer customer = null;
    private boolean isIdRead = false;
    private boolean isFnameRead = false;
    private boolean isEmailRead = false;

    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        String content = new String(ch, start, length);
        if (isIdRead) {
            customer.setId(content);
        } else if (isFnameRead) {
            customer.setFirstName(content);
        } else if (isEmailRead) {
            customer.setEmail(content);
        }
    }

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        if ("id".equalsIgnoreCase(qName)) {
            isIdRead = false;
        } else if ("fname".equalsIgnoreCase(qName)) {
            isFnameRead = false;
        } else if ("email".equalsIgnoreCase(qName)) {
            isEmailRead = false;
        }
    }

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        if ("customers".equalsIgnoreCase(qName)) {
            listCustomer = new ArrayList<Customer>();
        } else if ("customer".equalsIgnoreCase(qName)) {
            customer = new Customer();
            listCustomer.add(customer);
        } else if ("id".equalsIgnoreCase(qName)) {
            isIdRead = true;
        } else if ("fname".equalsIgnoreCase(qName)) {
            isFnameRead = true;
        } else if ("email".equalsIgnoreCase(qName)) {
            isEmailRead = true;
        }

    }

    public List<Customer> getListCustomer() {
        return listCustomer;
    }
}
